package com.tinysand.advert.service;

import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.DeleteRowsEventData;
import com.github.shyiko.mysql.binlog.event.EventData;
import com.github.shyiko.mysql.binlog.event.UpdateRowsEventData;
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author TINY
 */
@Slf4j
public class BinlogLauncher {
    public static void main(String[] args) throws IOException {
        BinaryLogClient client = new BinaryLogClient(
                "127.0.0.1", 3306, "root", "tiny"
        );
//        client.setBinlogFilename("");
        client.registerEventListener(event -> {
            EventData data = event.getData();

            if (data instanceof UpdateRowsEventData) {
                log.info("Mysql更新数据({})", data.toString());
            } else if (data instanceof WriteRowsEventData) {
                log.info("Mysql写入数据({})", data.toString());
            } else if (data instanceof DeleteRowsEventData) {
                log.info("Mysql删除数据（{})", data.toString());
            }
        });

        client.connect();
    }
}
